/**
 * 2015年10月18日19:48:42
 */

//Directives
var directives = angular.module('organization_directives', []);
directives.directive('ngMenuArea', function ($window) {
    return function (scope, element) {
        var w = angular.element($window);
        scope.getWindowDimensions = function () {
            return {'height': w.height(), 'width': w.width()};
        };
        scope.$watch(scope.getWindowDimensions, function (newValue, oldValue) {
            if(('ontouchstart' in window) || window.DocumentTouch && document instanceof DocumentTouch) {
                console.log("页面开启触屏兼容模式");
            }else {
                element.css({"height": newValue.height-50 +'px'});
            }
        }, true);
        w.bind('resize', function () {
            scope.$apply();
        });
    };
});
directives.directive('ngContentLeft', function ($window) {
    return function (scope, element) {
//        var contentLeftScroll;
        var w = angular.element($window);
//        element.niceScroll({
//            cursorborder:0,cursorborderradius:0,cursoropacitymax:0,cursorwidth:0
//        });
        element.bind('mouseover touchstart',function(){
            scope.$apply();
        });
        scope.getWindowDimensions = function () {
            return {'height': w.height(), 'width': w.width()};
        };
        scope.$watch(scope.getWindowDimensions, function (newValue, oldValue) {
            if(('ontouchstart' in window) || window.DocumentTouch && document instanceof DocumentTouch) {
                console.log("页面开启触屏兼容模式");
                if (newValue.width < (512 + 320)) {
                    element.css({"display": "none"});
                } else {
                    element.css({"display": "block"});
                }
            }else {
                if (newValue.width < (512 + 320)) {
                    element.css({"display": "none"});
                } else {
                    element.css({"display": "block", "height": newValue.height - 50 + 'px'});
                }
//
//                if(contentLeftScroll!=null)
//                    contentLeftScroll.destroy();
//                contentLeftScroll = new IScroll('#contentLeft',{ mouseWheel: true, tap: true });
            }
        }, true);
        w.bind('resize', function () {
            scope.$apply();
        });
    };
});
directives.directive('ngContentRight', function ($window) {
    return function (scope, element) {
        //var contentRightScroll;
        var w = angular.element($window);

        scope.getWindowDimensions = function () {
            return {'height': w.height(), 'width': w.width()};
        };
        scope.$watch(scope.getWindowDimensions, function (newValue, oldValue) {
            if(('ontouchstart' in window) || window.DocumentTouch && document instanceof DocumentTouch) {
                console.log("页面开启触屏兼容模式");
                if (newValue.width< (512+320)) {
                    element.css({"width":newValue.width+"px"});
                }else{
                    element.css({"width":newValue.width-320 + 'px'});
                }
            }else {
                if (newValue.width< (512+320)) {
                    element.css({"width":newValue.width+"px","height": newValue.height-50+ 'px'});
                }else{
                    element.css({"width":newValue.width-320 + 'px',"height": newValue.height-50 + 'px'});
                }
//                if(contentRightScroll!=null)
//                    contentRightScroll.destroy();
//                contentRightScroll = new IScroll('#contentRight',{ mouseWheel: true, tap: true });
            }

        }, true);
        //element.niceScroll({cursorborder:0,cursorborderradius:0,cursoropacitymax:0,cursorwidth:0});
        w.bind('resize', function () {
            scope.$apply();
        });
    };
});
directives.directive('ngContentRightWrap', function ($window) {
    return function (scope, element) {
        var w = angular.element($window);

        scope.getWindowDimensions = function () {
            return {'height': w.height(), 'width': w.width()};
        };
        scope.$watch(scope.getWindowDimensions, function (newValue, oldValue) {
            if(('ontouchstart' in window) || window.DocumentTouch && document instanceof DocumentTouch) {
                console.log("页面开启触屏兼容模式");
            }else {
                if (newValue.width< (512+320)) {
                    element.css({"height": newValue.height-50 -40 + 'px'});
                }else{
                    element.css({"height": newValue.height-50 -40 + 'px'});
                }
            }

        }, true);
        w.bind('resize', function () {
            scope.$apply();
        });
    };
});

directives.directive('ngContentRightMenuBarRightDropdown', function () {
    return function (scope, element) {
        var $dropdown = $('#contentRightMenuBarRightDropdown');

        $dropdown.dropdown({justify: '#contentRightMenuBarRightDropdownWrap'});
        element.find("button").click(function(){
            $dropdown.dropdown('toggle');
        });



    };
});

directives.directive('ngOrganizationNodeContentLeftDateFilter', function () {
    return function (scope, element) {
        var startDate=null;
        var endDate=null;
        var $alert = $('#organizationNodeContentLeftDateFilterAlert');
        $('#organizationNodeContentLeftDateFilterStartDate').datepicker().
            on('changeDate.datepicker.amui', function(event) {
                if(endDate==null){
                    $alert.hide();
                    startDate = new Date(event.date);
                    scope.organizationNodeContentLeftDateFilterStartDateInput = startDate.getTime();
                    scope.$apply();
                }else if(event.date.valueOf() > endDate.valueOf()){
                        $alert.find('p').text('开始日期应小于结束日期！').end().show();
                }else {
                    $alert.hide();
                    startDate = new Date(event.date);
                    scope.organizationNodeContentLeftDateFilterStartDateInput = startDate.getTime();
                    scope.$apply();
                }
                $(this).datepicker('close');
            });
        $('#organizationNodeContentLeftDateFilterEndDate').datepicker().
            on('changeDate.datepicker.amui', function(event) {

            if(startDate==null){
                        $alert.hide();
                        endDate = new Date(event.date);
                        scope.organizationNodeContentLeftDateFilterEndDateInput = endDate.getTime();
                        scope.$apply();
            }else if(event.date.valueOf() < startDate.valueOf()){
                        $alert.find('p').text('结束日期应大于开始日期！').end().show();
            }else {
                    $alert.hide();
                    endDate = new Date(event.date);
                    scope.organizationNodeContentLeftDateFilterEndDateInput = endDate.getTime();
                    scope.$apply();
                }
                $(this).datepicker('close');
            });
        $('#organizationNodeContentLeftDateFilterStart').click(function(event) {
            $('#organizationNodeContentLeftDateFilterStartDate').val(null);
            startDate=null;
            scope.organizationNodeContentLeftDateFilterStartDateInput =null;
            scope.$apply();
            $alert.hide();
        });
        $('#organizationNodeContentLeftDateFilterEnd').click(function(event) {
            $('#organizationNodeContentLeftDateFilterEndDate').val(null);
            endDate=null;
            scope.organizationNodeContentLeftDateFilterEndDateInput =null;
            scope.$apply();
            $alert.hide();
        });

    };
});

directives.directive('ngPositionContentLeftDateFilter', function () {
    return function (scope, element) {
        var startDate=null;
        var endDate=null;
        var $alert = $('#positionContentLeftDateFilterAlert');
        $('#positionContentLeftDateFilterStartDate').datepicker().
            on('changeDate.datepicker.amui', function(event) {
                if(endDate==null){
                    $alert.hide();
                    startDate = new Date(event.date);
                    scope.positionContentLeftDateFilterStartDateInput = startDate.getTime();
                    scope.$apply();
                }else if(event.date.valueOf() > endDate.valueOf()){
                    $alert.find('p').text('开始日期应小于结束日期！').end().show();
                }else {
                    $alert.hide();
                    startDate = new Date(event.date);
                    scope.positionContentLeftDateFilterStartDateInput = startDate.getTime();
                    scope.$apply();
                }
                $(this).datepicker('close');
            });
        $('#positionContentLeftDateFilterEndDate').datepicker().
            on('changeDate.datepicker.amui', function(event) {

                if(startDate==null){
                    $alert.hide();
                    endDate = new Date(event.date);
                    scope.positionContentLeftDateFilterEndDateInput = endDate.getTime();
                    scope.$apply();
                }else if(event.date.valueOf() < startDate.valueOf()){
                    $alert.find('p').text('结束日期应大于开始日期！').end().show();
                }else {
                    $alert.hide();
                    endDate = new Date(event.date);
                    scope.positionContentLeftDateFilterEndDateInput = endDate.getTime();
                    scope.$apply();
                }
                $(this).datepicker('close');
            });
        $('#positionContentLeftDateFilterStart').click(function(event) {
            $('#positionContentLeftDateFilterStartDate').val(null);
            startDate=null;
            scope.positionContentLeftDateFilterStartDateInput =null;
            scope.$apply();
            $alert.hide();
        });
        $('#positionContentLeftDateFilterEnd').click(function(event) {
            $('#positionContentLeftDateFilterEndDate').val(null);
            endDate=null;
            scope.positionContentLeftDateFilterEndDateInput =null;
            scope.$apply();
            $alert.hide();
        });

    };
});



directives.directive('ngBootStrapSwitch', function () {
    return function (scope, element) {
        element.bootstrapSwitch({lableText:element.data("labeltext")});
    };
});
directives.directive('ngAmazeSelectUp',function(){
    return function (scope, element) {
        element.selected({dropUp: 1});
    };
});